package cn.tedu.imop.mq.listener;
import cn.tedu.imop.mq.config.DeadLetterQueueConfig;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalTime;

@Component
public class SpringRabbitListener {
    /**在此对象监听到rabbit指定队列(simple.queue)有消息后，
     * 会回调listenSimpleQueueMessage函数
     */
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue1(String msg) throws InterruptedException {
//        //System.out.println("spring 消费者接收到消息：【" + msg + "】");
//        System.err.println("消费者1........接收到消息：【" + msg + "】" + LocalTime.now());
//        Thread.sleep(1000);
//    }
//    @RabbitListener(queues = "simple.queue")
//    public void listenWorkQueue2(String msg) throws InterruptedException {
//        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
//        Thread.sleep(2000);
//    }
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg) {
        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "exc.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void listenDirectQueue1(String msg){
        System.out.println("消费者1接收到direct.queue1的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "exc.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void listenDirectQueue2(String msg){
        System.out.println("消费者2接收到direct.queue2的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "exc.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void listenTopicQueue1(String msg){
        System.out.println("消费者1接收到topic.queue1的消息：【" + msg + "】");
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "exc.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void listenTopicQueue2(String msg){
        System.out.println("消费者2接收到topic.queue2的消息：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue3"),
            exchange = @Exchange(name = "exc.topic", type = ExchangeTypes.TOPIC),
            key = "#.object"
    ))
    public void listenTopicQueue3(String msg){
        System.out.println("消费者3接收到topic.queue2的消息：【" + msg + "】");
    }

    @RabbitListener(queuesToDeclare = @org.springframework.amqp.rabbit.annotation.Queue(
            name = "lazy.queue2",
            durable = "true",
            arguments = @Argument(
                    name = "x-queue-mode",
                    value = "lazy"
            )
    ))
    public void listenLazeQueue(String message) {
        System.out.println("消费者收到了 lazy.queue2的消息: " + message);
    }

//    @RabbitListener(queues = "simple.queue",ackMode = "MANUAL")
//    public void listenSimpleQueue(Message message, Channel channel) {
//        System.out.println("消费者收到了simple.queue的消息：【" + message.getBody() + "】");
//        //throw new MessageConversionException("故意抛出异常");
//        try {
//            //获取消息的唯一标识
//            long deliveryTag = message.getMessageProperties().getDeliveryTag();
//            System.out.println("deliveryTag="+deliveryTag);
//            channel.basicAck(deliveryTag,false);//false标识是否确认小于唯一标识的其它消息
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String message) {
        System.out.println("消费者收到了simple.queue的消息：【" + message + "】");
        //throw new RuntimeException("故意抛出异常");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue"),
            exchange = @Exchange(name = "delay.direct", delayed = "true", type = ExchangeTypes.DIRECT),
            key = "delay"
    ))
    public void listenDelayQueue(String message) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss SSS");
        System.out.println("消费者收到了 delay.queue的消息: " + message + "，时间：" + simpleDateFormat.format(System.currentTimeMillis()));
    }

    @RabbitListener(queues = DeadLetterQueueConfig.DEAD_LETTER_QUEUE)
    public void receiveDeadLetterMessage(String message) {
        System.out.println("Received dead letter message: " + message);
    }



}